﻿/*
 *   Copyright 2011 Peter Crossley
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 *  ----------------------------------------------------------------------------
 *  File:       ProcessScript.cs
 *  Author:     Peter Crossley
 *  ----------------------------------------------------------------------------
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace UEC.Modules.Configuration
{
    public class ProcessScript
    {
        private string _script;

        [XmlAttribute("Name")]
        public string Name;
        [XmlAttribute("Type")]
        public ScriptType Type = ScriptType.RUN_ONCE;

        [XmlAttribute("Interpreter")]
        public string Interpreter;

        [XmlAttribute("Elevate")]
        public bool Elevate = false;

        [XmlAttribute("Arguments")]
        public string Arguments = "";

        [XmlAttribute("Extention")]
        public string Extention = "bat";

        [XmlAttribute("Interactive")]
        public bool Interactive = false;

        [XmlAttribute("AllowReboot")]
        public bool AllowReboot = false;

        [XmlAttribute("Weight")]
        public int Weight = 0;

        [XmlAttribute("Source")]
        public string Source;

        [XmlText()]
        public XmlNode[] ScriptXML
        {
            get
            {
                if (_script == null)
                {
                    return new XmlNode[0];
                }

                var dummy = new XmlDocument();
                return new XmlNode[] { dummy.CreateCDataSection(_script) };

            }

            set
            {

                if (value == null)
                {
                    _script = null;
                    return;
                }

                if (value.Length != 1)
                {
                    throw new InvalidOperationException(
                        String.Format(
                            "Invalid array length {0}", value.Length));
                }

                var node0 = value[0];
                if (node0 == null)
                {
                    throw new InvalidOperationException(
                    String.Format(
                            "Invalid node type {0}", node0.NodeType));
                }
                _script = node0.Value;
            }
        }

        [XmlIgnore]
        public string Script
        {
            get
            {
                if (ScriptXML == null || ScriptXML.Length == 0)
                {
                    return null;
                }
                return ScriptXML[0].Value;
            }

            set
            {

                if (value == null)
                {
                    ScriptXML = null;
                    return;
                }
                Source = null;

                var dummy = new XmlDocument();
                ScriptXML = new XmlNode[] { dummy.CreateCDataSection(value) };
            }
        }



        public enum ScriptType
        {
            USER,
            SYSTEM,
            RUN_ONCE
        }
    }

}
